/*
 *  Kara IM - Kara Instant Messenger
 *  Copyright (C) 2007-2008 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.karaim;

import java.util.Vector;

import de.karnik.helper.LogFunctions;

import android.view.Menu;
import android.view.SubMenu;
import android.view.Menu.Item;
import android.view.Menu.OnClickListener;

/**
 * This class creates an manages the Kara IM main menu.
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class KaraMenu implements OnClickListener {
	
	// define static menu entries
    /**
     * The status menu entry position.
     */
    public static final int MENUID_STATE = Menu.FIRST;
    /**
     * The preferences menu entry position.
     */
    public static final int MENUID_PREFERENCES = Menu.FIRST + 1;
    /**
     * The about menu entry position.
     */
    public static final int MENUID_ABOUT = Menu.FIRST + 2;
    //public static final int MENUID_SEPERATOR = Menu.FIRST + 3;
    
    // submenu state entries
    /**
     * The available menu entry position.
     */
    public static final int MENUID_AVAILABLE = Menu.FIRST + 4;
    /**
     * The dnd menu entry position.
     */
    public static final int MENUID_DND = Menu.FIRST + 5;
    /**
     * The away menu entry position.
     */
    public static final int MENUID_AWAY = Menu.FIRST + 6;
    /**
     * The invisible menu entry position.
     */
    public static final int MENUID_INVISIBLE = Menu.FIRST + 7;
    /**
     * The exit menu entry position.
     */
    public static final int MENUID_OFFLINE = Menu.FIRST + 8;
    
    public static final int MENUID_EXIT = Menu.FIRST + 40;
	
	/**
	 * The main menu.
	 */
	private Menu menu;
	/**
	 * The menu listeners.
	 */
	private Vector<MenuListener> menuListener = new Vector<MenuListener>();
	
	/**
	 * Creates KaraMenu object an builds the main menu. The structure will be stored in the given menu object.
	 * 
	 * @param menu The Kara IM main menu.
	 */
	public KaraMenu( Menu menu ) {
		this.menu = menu;
		buildInitial();
	}
	
	/**
	 * Adds a MenuLister to the listener vector.
	 * 
	 * @param menuListener The MenuListener to add.
	 */
	public void addMenuListener( MenuListener menuListener ) {
		this.menuListener.add( menuListener );
	}
	
	/**
	 * Build the initial menu structure
	 */
	public void buildInitial() {
		
		menu.add( 0, MENUID_EXIT, R.string.menu_exit ).setClickListener( this );
		//menu.addSeparator( 0, MENUID_SEPERATOR );
		
		SubMenu subMenu = menu.addSubMenu( 0, MENUID_STATE, R.string.menu_state );
        
		subMenu.add( 0, MENUID_AVAILABLE, R.string.menu_available ).setClickListener( this );
		subMenu.add( 0, MENUID_AWAY, R.string.menu_away ).setClickListener( this );
		subMenu.add( 0, MENUID_DND, R.string.menu_dnd ).setClickListener( this );
		subMenu.add( 0, MENUID_DND, R.string.menu_invisible ).setClickListener( this );
		subMenu.add( 0, MENUID_OFFLINE, R.string.menu_offline ).setClickListener( this );
		
		//menu.addSeparator( 0, MENUID_SEPERATOR );
		menu.add( 0, MENUID_PREFERENCES, R.string.menu_preferences ).setClickListener( this );
		menu.add( 0, MENUID_ABOUT, R.string.menu_about ).setClickListener( this );
		

    }

	/* (non-Javadoc)
	 * @see android.view.Menu$OnClickListener#onClick(android.view.Menu.Item)
	 */
	public boolean onClick( Item item ) {
		
		if( item.getId() == MENUID_PREFERENCES ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_PREFERENCES clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).preferencesClicked();			
		}
		
		if( item.getId() == MENUID_ABOUT ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_ABOUT clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).aboutClicked();			
		}
		
		if( item.getId() == MENUID_AVAILABLE ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_AVAILABLE clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).availableClicked();
		}

		if( item.getId() == MENUID_AWAY ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_AWAY clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).awayClicked();			
		}
		
		if( item.getId() == MENUID_DND ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_DND clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).dndClicked();			
		}
		
		if( item.getId() == MENUID_INVISIBLE ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_INVISIBLE clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).invisibleClicked();			
		}
		
		if( item.getId() == MENUID_OFFLINE ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_OFFLINE clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).offlineClicked();			
		}
		
		if( item.getId() == MENUID_EXIT ) {
			LogFunctions.log( "KaraMenu.onClick", "MENUID_EXIT clicked" );
			for( int i = 0; i < this.menuListener.size(); i++ )
				this.menuListener.get( i ).exitClicked();			
		}		
		
		
		return false;
	}
	
}
